package com.zh.elec.web.actions;

import java.util.Date;
import java.util.List;

import com.zh.elec.model.Users;
import com.zh.elec.service.UsersService;
import com.zh.elec.web.actions.base.AbstractAction;

/**
 * 登录
 * @author user
 *
 */
public class LoginAction extends AbstractAction {
	/**
	 * 
	 */
	private static final long serialVersionUID = -7428514682690466118L;
	private UsersService usersService;
	private Users users;
	
	public String login(){
		if(users != null && !users.getUsername().isEmpty() && !users.getPassword().isEmpty()){
			List<Users> list = usersService.findByName("username", users.getUsername());
			if(list.isEmpty()){
				super.addFieldError("username", "用户名不存在!");
				return "login";
			}else{
				Users u = list.get(0);
				if(u.getPassword().equals(users.getPassword())){
					return "loginSuccess";
				}else{
					super.addFieldError("password", "密码错误!");
					return "login";
				}
			}
		}else{
			super.addFieldError("username", "用户名或密码不能为空,请输入用户名和密码!");
			return "login";
		}
	}
	
	public String save(){
		if(users != null){
			users.setInsertTime(new Date());
			this.usersService.save(users);
		}		
		return "success";
	}
	
	
	public void validate(){
		if(users == null || users.getUsername().isEmpty()){
			super.addActionError("用户名不能为空!");
		}
		if(users == null || users.getPassword().isEmpty()){
			super.addActionError("密码不能为空!");
		}
	}

	public Users getUsers() {
		return users;
	}

	public void setUsers(Users users) {
		this.users = users;
	}
	public void setUsersService(UsersService usersService) {
		this.usersService = usersService;
	}
}
